﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.WriteAPI.Applications.Commands.Users;
using MediatR;

namespace B.S.BaseData.WriteAPI.Applications.Handlers.Users
{
    public class DeleteUserHandler:IRequestHandler<DeleteUserCommand, ApiResult>
    {
        private readonly IUserRepository userRep;

        public DeleteUserHandler(IUserRepository userRep)
        {
            this.userRep = userRep;
        }
        public async Task<ApiResult> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
           var info = await userRep.GetByIdAsync(request.UserId);
            if (info == null)
            {
                return ApiResult.Fail("用户不存在", StatusCodeEnum.Fail);
            }
            // 删除用户
            var result = await userRep.DeleteAsync(info);
            if (result > 0)
            {
                return ApiResult.Success(StatusCodeEnum.Ok);
            }
            else
            {
                return ApiResult.Fail("删除用户失败", StatusCodeEnum.Fail);
            }
        }
    }
}
